import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import Qt5Compat.GraphicalEffects
import FluentUI

import "../components/"
import MyApp
import DeviceIns

ColumnLayout {
    property var cur_dev: client.cur_device_index
                          === 255 ? blank_device : client.devices[client.cur_device_index]
    DeviceIns {
        id: blank_device
    }
    RowLayout {
        FluText {
            text: "测量终端 "
            font.pixelSize: 24
            font.bold: true
        }
        RowLayout {
            Layout.alignment: Qt.AlignBottom
            FluRectangle {
                Layout.alignment: Qt.AlignVCenter
                height: 12
                width: 12
                radius: [6, 6, 6, 6]
                color: {
                    switch (cur_dev.device_status) {
                    case Types.IDLE:
                        return "green"
                    case Types.MEASURING:
                        return "yellow"
                    case Types.UNKNOWN:
                        return "yellow"
                    case Types.TIMEOUT:
                        return "red"
                    case Types.DISCONNECTED:
                        return "red"
                    default:
                        return "red"
                    }
                }
            }
            FluText {
                id: status_label
                Layout.alignment: Qt.AlignVCenter
                property string currentText
                currentText: {
                    switch (cur_dev.device_status) {
                    case Types.IDLE:
                        return "IDLE"
                    case Types.MEASURING:
                        return "MEASURE"
                    case Types.UNKNOWN:
                        return "WAITING"
                    case Types.TIMEOUT:
                        return "TIMEOUT"
                    case Types.DISCONNECTED:
                        return "OFFLINE"
                    default:
                        return "未选择"
                    }
                }

                font.pixelSize: 18
                opacity: 1.0

                SequentialAnimation {
                    id: fadeAnimation
                    running: false
                    NumberAnimation {
                        target: status_label
                        property: "opacity"
                        to: 0
                        duration: 100 // 淡出动画
                    }
                    ScriptAction {
                        script: {
                            // 在淡出结束时更新文本
                            status_label.text = status_label.currentText // 保存当前的文本内容
                        }
                    }
                    NumberAnimation {
                        target: status_label
                        property: "opacity"
                        to: 1
                        duration: 100 // 淡入动画
                    }
                }
                // 每次文字变化时触发淡出和淡入
                onCurrentTextChanged: {
                    fadeAnimation.start()
                }
            }
        }
    }
    RowLayout {
        Layout.fillHeight: true
        spacing: 20
        ColumnLayout {
            Layout.fillHeight: true
            Layout.fillWidth: true
            Layout.alignment: Qt.AlignTop
            FluText {
                Layout.topMargin: 5
                text: "当前"
                font.pixelSize: 16
                font.bold: true
            }

            GridLayout {
                Layout.fillWidth: true
                columns: 2
                rows: 4
                FluText {
                    text: "电压："
                    font.pixelSize: 14
                }
                FluText {
                    Layout.alignment: Qt.AlignRight
                    text: roundToDecimals(cur_dev.cur_voltage, 2) + "V"
                    font.pixelSize: 14
                }
                FluText {
                    text: "测量状态："
                    font.pixelSize: 14
                }
                FluText {
                    Layout.alignment: Qt.AlignRight
                    text: {
                        switch (cur_dev.task_type) {
                        case Types.NONE:
                            return "空闲"
                        case Types.GET_VOLTAGE:
                            return "获取电压"
                        case Types.GET_SINGLE:
                            return "单点测量"
                        case Types.GET_EIS:
                            return "EIS测量"
                        default:
                            return "未知任务"
                        }
                    }

                    font.pixelSize: 14
                }
                FluText {
                    text: "电流："
                    font.pixelSize: 14
                }
                FluText {
                    Layout.alignment: Qt.AlignRight
                    text: {
                        switch (cur_dev.cur_current) {
                        case Types.AMPL_1A:
                            return "1A"
                        case Types.AMPL_A15:
                            return "0.15A"
                        case Types.AMPL_A25:
                            return "0.25A"
                        case Types.AMPL_A5:
                            return "0.5A"
                        case Types.AMPL_A8:
                            return "0.8A"
                        }
                    }
                    font.pixelSize: 14
                }
                FluText {
                    text: "频率："
                    font.pixelSize: 14
                }
                FluText {
                    Layout.alignment: Qt.AlignRight
                    text: cur_dev.freq + "Hz"
                    font.pixelSize: 14
                }
                FluProgressBar {
                    Layout.preferredWidth: 180
                    Layout.columnSpan: 2
                    // indeterminate: cur_dev.task_type === Types.GET_SINGLE
                    indeterminate: false
                    value: cur_dev.progress / 100
                }
            }

            FluText {
                Layout.topMargin: 10
                text: "控制"
                font.pixelSize: 16
                font.bold: true
            }
            GridLayout {
                columns: 2
                rows: 4
                FluText {
                    text: "模式："
                    font.pixelSize: 14
                }
                FluComboBox {
                    id: mode_combo
                    Layout.preferredWidth: 125
                    model: ListModel {
                        ListElement {
                            text: "单点测量"
                        }
                        ListElement {
                            text: "阻抗图谱"
                        }
                        // ListElement {
                        //     text: "欧姆阻抗"
                        // }
                    }
                }

                FluText {
                    text: "电流："
                    font.pixelSize: 14
                }
                FluComboBox {
                    id: current_combo
                    Layout.preferredWidth: 120
                    model: ListModel {
                        ListElement {
                            text: "0.15A"
                        }
                        ListElement {
                            text: "0.25A"
                        }
                        ListElement {
                            text: "0.5A"
                        }
                        ListElement {
                            text: "0.8A"
                        }
                        ListElement {
                            text: "1A"
                        }
                    }
                }

                FluText {
                    text: "频率："
                    font.pixelSize: 14
                }
                FluSpinBox {
                    id: freq_spin
                    Layout.preferredWidth: 120
                    value: 100
                    editable: true
                    from: 1
                    to: 2000
                    stepSize: 100
                }

                Item {
                    Layout.columnSpan: 2
                    Layout.preferredHeight: 1
                }

                Layout.fillWidth: true
                FluText {
                    // Layout.alignment: Qt.AlignBottom
                    text: cur_dev.device_status === Types.IDLE ? "- 就绪 -" : "- 忙碌 -"
                    font.pixelSize: 14
                }
                FluLoadingButton {
                    // Layout.fillWidth: true
                    Layout.alignment: Qt.AlignRight
                    text: "测量"
                    loading: cur_dev.device_status !== Types.IDLE
                    onClicked: {
                        cur_dev.measure(mode_combo.currentIndex+2,
                                        current_combo.currentIndex,
                                        freq_spin.value)
                    }
                }
            }
        }

        ColumnLayout {
            Layout.fillHeight: true

            Results {
                device: cur_dev

                Layout.fillHeight: true
                Layout.fillWidth: true
            }
        }
    }
    ColumnLayout {
        Layout.fillHeight: true
        RowLayout {
            FluText {
                text: "任务"
                font.pixelSize: 16
                font.bold: true
            }
            Item {
                Layout.fillWidth: true
            }
            FluIconButton {
                iconSize: 16
                iconSource: FluentIcons.Add
                onClicked: {

                }
            }
            FluIconButton {
                iconSize: 16
                iconSource: FluentIcons.Clear
                onClicked: {

                }
            }
        }

        TaskTable {
            Layout.preferredHeight: 200
            // Layout.fillHeight: true
            Layout.fillWidth: true
            device: cur_dev
        }
    }

    function roundToDecimals(num, decimals) {
        var factor = Math.pow(10, decimals)
        return Math.round(num * factor) / factor
    }
}
